import requests
from concurrent.futures import ThreadPoolExecutor


# 并发执行的任务函数
def post_user(openid):
    url = "http://127.0.0.1:1479/mkmpjk/frontapp/add_test"
    data = {"openid": openid}
    response = requests.post(url, data=data)
    data = response.json()
    return data


# 并发测试函数
def concurrent_test():
    # 假设有3000个任务需要并发执行
    num_tasks = 3000

    # 使用ThreadPoolExecutor创建一个线程池
    with ThreadPoolExecutor(max_workers=10) as executor:
        # 提交任务到线程池并发执行
        future_to_openid = {executor.submit(post_user, str(i)): str(i) for i in range(num_tasks)}

        # 获取并输出任务的结果
        for future in concurrent.futures.as_completed(future_to_openid):
            openid = future_to_openid[future]
            try:
                data = future.result()
                print(f"任务{openid}执行成功：{data}")
            except Exception as e:
                print(f"任务{openid}执行失败：{e}")


if __name__ == "__main__":
    concurrent_test()
